package common.bean;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;

import common.constant.GlobalConstant;
import common.util.JavaBeanUtils;

/**
 * 封装分页数据
 * 
 * @author bianj
 * @version 1.0.0 2017-07-15
 */
public class Paging extends ToStringBean {
    private static final long serialVersionUID = -5726914803574438815L;

    /** 每页显示多少条记录 */
    private Integer pageSize = GlobalConstant.DEFAULT_PAGE_SIZE;
    private int code = 0;
    private  String msg="获取数据";

    public int getCode(){return code;}
    public String getMsg(){return msg;}
    /** 当前页 */
    private Integer page = 1;

    /** 总记录数 */
    private Long total = 0l;

    /** 排序字段 */
    private String sort;

    /** 排序方式 ，只能是ASC或DESC */
    private String order;

    /** 查询参数 */
    private Map<String, ?> params;

    /** 当前这一页的记录集 */
    private List<?> rows;

    public Integer getBeginCount() {
        int beginCount = 0;
        if (page > 0 && pageSize > 0) {
            beginCount = pageSize * (page - 1);
        }

        return beginCount;
    }

    public Integer getPageSize() {
        return pageSize;
    }
    public Integer getLimit(){
            return pageSize;
    }
    public void setPageSize(Integer pageSize) {
        if (pageSize != null && pageSize > 0) {
            this.pageSize = pageSize;
        }
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        if (page != null && page > 0) {
            this.page = page;
        }
    }

    public List<?> getRows() {
        return rows;
    }
    public List<?> getData() {
        return rows;
    }


    public void setRows(List<?> rows) {
        this.rows = rows;
    }

    public Long getCount(){return total ;}
    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        if (total != null && total >= 0) {
            this.total = total;
        }
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    public Map<String, ?> getParams() {
        return params;
    }

    public void setParams(Map<String, ?> params) {
        this.params = params;
    }

    public void setStringParams(Object javaBean) {
        this.params = JavaBeanUtils.beanToStringMap(javaBean);
    }

    public void setObjectParams(Object javaBean) {
        this.params = JavaBeanUtils.beanToObjectMap(javaBean);
    }
    
    public void clearRows(){
        if (CollectionUtils.isNotEmpty(rows)) {
            rows.clear();
        }
    }
}
